<template>
  <el-aside
    class="vip-column-bar"
    :class="{
      'is-collapse': collapse,
      ['vip-column-bar-' + theme.columnStyle]: true,
    }"
  >

    <!--        <div class="vip-logo-wrapper">-->
    <!--          <vip-logo />-->
    <!--        </div>-->
<!--    <vip-logo />-->

    <div class="vip-column-bar-wrapper">
      <el-aside class="vip-column-bar-left">
        <div class="vip-column-bar-left-scroll">
          <el-scrollbar>
            <el-tabs v-model="tab.data" tab-position="left" @tab-click="handleTabClick">
              <template v-for="(item, index) in routes" :key="index + item.name">
                <el-tab-pane :name="item.name">
                  <template #label>
                    <div
                      class="vip-column-grid"
                      :class="{
                    ['vip-column-grid-' + theme.columnStyle]: true,
                  }"
                      :title="translate(item.meta.title)"
                    >
                      <div>
                        <vip-icon v-if="item.meta.icon" :icon="item.meta.icon" :is-custom-svg="item.meta.isCustomSvg" />
                        <span v-if="translate(item.meta.title).length < 4">
                      {{ translate(item.meta.title) }}
                    </span>
                        <span v-else style="font-size: var(--el-font-size-small); zoom: 0.88">
                      {{ translate(item.meta.title) }}
                    </span>
                      </div>
                    </div>
                  </template>
                </el-tab-pane>
              </template>
            </el-tabs>
          </el-scrollbar>

        </div>
      </el-aside>

      <el-aside class="vip-column-bar-right"
                :class="{
                  'is-collapse': collapse
                }">

        <div class="vip-column-bar-right-scroll">
          <el-scrollbar>
            <el-menu
              ref="menuRef"
              background-color="var(--el-menu-background-color-second)"
              :default-active="activeMenu.data"
              :default-openeds="defaultOpeneds"
              :unique-opened="uniqueOpened"
              mode="vertical"
            >
              <vip-menu v-for="item in partialRoutes" :key="item.path" :item="item" />
              <!--      <vab-menu v-for="item in partialRoutes" :key="item.path" :item="item" />-->
            </el-menu>
          </el-scrollbar>
        </div>

        <vip-menu-float-fold />

      </el-aside>

    </div>
  </el-aside>
</template>

<script setup lang="ts">


import { defaultOpeneds, isHashRouterMode, openFirstMenu, uniqueOpened } from '/@lib/config'
import { translate } from '/@/i18n'
import { useRoutesStore } from '/@/store/modules/routes'
import { useSettingsStore } from '/@/store/modules/settings'
import { isExternal } from '/@lib/utils/validate'


defineOptions({
  name: 'VipColumnBar',
})

const route: AppRoute = useRoute()
const router = useRouter()
const settingsStore = useSettingsStore()
const { collapse, device, theme } = storeToRefs(settingsStore)
const { foldSideBar, openSideBar } = settingsStore
const routesStore = useRoutesStore()
const {
  getTab: tab,
  getTabMenu: tabMenu,
  getActiveMenu: activeMenu,
  getRoutes: routes,
  getPartialRoutes: partialRoutes,
} = storeToRefs<any>(routesStore)
const menuRef = ref<any>(null)

const setDefaultOpeneds = () => {
  useTimeoutFn(() => {
    defaultOpeneds.forEach((item: string) => {
      try {
        menuRef.value.open(item)
      } catch (e) {
        /* empty */
      }
    })
  }, 0)
}

const handleTabClick = () => {
  nextTick(() => {
    if (tabMenu.value.meta.target === '_blank') {
      if (route.path !== tabMenu.value.path) {
        isHashRouterMode ? window.open(`#${tabMenu.value.path}`) : window.open(tabMenu.value.path)
        router.push('/redirect')
      }
    } else if (isExternal(tabMenu.value.path)) {
      window.open(tabMenu.value.path)
      router.push('/redirect')
    } else if (openFirstMenu) router.push(tabMenu.value.redirect || tabMenu.value)
    setDefaultOpeneds()
  })
}

onMounted(() => {
  nextTick(() => {
    setDefaultOpeneds()
    if (theme.value.layout === 'column')
      watch(
        route,
        () => {
          const foldUnfold: any = document.querySelector('.left-panel .fold-unfold')
          const floatFold: any = document.querySelector('.float-fold')
          if (route.meta.noColumn && theme.value.layout === 'column') {
            if (device.value !== 'mobile') foldSideBar()
            if (foldUnfold) foldUnfold.style = 'display:none'
            if (floatFold) floatFold.style = 'display:none'
          } else {
            if (device.value !== 'mobile') openSideBar()
            if (foldUnfold) foldUnfold.style = ''
            if (floatFold) floatFold.style = ''
          }
        },
        {
          immediate: true,
        },
      )
  })
})
</script>


<style scoped lang="scss">

@mixin active {
  &:hover {
    color: var(--el-color-primary);
    background-color: var(--el-color-primary-light-9);

    i,
    svg {
      color: var(--el-color-primary);
    }
  }

  &.is-active {
    color: var(--el-color-primary);
    background-color: var(--el-color-primary-light-9);
  }
}

.vip-column-bar {
  //position: relative;
  //height: 100%;
  height: 100vh;
  width: var(--el-left-menu-width);
  background: var(--el-color-white);
  //border-right: 1px solid var(--el-border-color);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: var(--el-transition);

  .vip-logo-wrapper {
    display: flex;
    flex-direction: row;
    flex-shrink: 0;
  }

  .vip-column-bar-wrapper {
    display: flex;
    flex-direction: row;
    //height: 100%;
    flex: 1;
    overflow: hidden;
  }

  .vip-column-bar-left {
    //position: relative;
    display: flex;
    flex-flow: column;
    flex-shrink: 0;
    //width: calc(var(--el-left-menu-width-min)) !important;
    //background: var(--el-menu-background-color);

    .vip-column-bar-left-scroll {
      overflow-x: hidden;
      flex: 1;
    }
  }

  .vip-column-bar-right {
    display: flex;
    flex-direction: column;
    flex: 1;
    //width: calc(var(--el-left-menu-width) - var(--el-left-menu-width-min));

    .vip-column-bar-right-scroll {
      overflow: auto;
      overflow-x: hidden;
      flex: 1;
      box-shadow: 2px 0 8px 0 rgba(29, 35, 41, 0.05);
      border-right: 1px solid var(--el-border-color);
    }
  }

  :deep() {
    .menu-float-fold {
      left: var(--el-left-menu-width) !important;
    }
  }

}


.vip-column-bar {

  &-vertical,
  &-card,
  &-arrow {
    .vip-column-bar-left {
      width: var(--el-left-menu-width-min);
    }

    .vip-column-bar-right {
      width: calc(var(--el-left-menu-width) - var(--el-left-menu-width-min));
    }

    &.is-collapse {
      :deep() {
        width: var(--el-left-menu-width-min);

        .menu-float-fold {
          left: calc(var(--el-left-menu-width-min)) !important;
        }

        .vip-logo-column {
          .title {
            width: 0 !important;
          }
        }

      }
    }
  }

  &-horizontal,
  &-semicircle {

    .vip-column-bar-left {
      width: calc(var(--el-left-menu-width-min) * 1.4);
    }

    .vip-column-bar-right {
      width: calc(var(--el-left-menu-width) - var(--el-left-menu-width-min) * 1.4);
    }

    &.is-collapse {
      :deep() {
        //width: 0;
        width: calc(var(--el-left-menu-width-min) * 1.4);

        .menu-float-fold {
          left: calc(var(--el-left-menu-width-min) * 1.4) !important;
        }

        .vip-logo-column {
          .logo {
            width: calc(var(--el-left-menu-width-min) * 1.4) !important;
          }

          .title {
            width: 0 !important;
            //left: calc(var(--el-left-menu-width-min) * 1.4) !important;
            //width: calc(var(--el-left-menu-width) - calc(var(--el-left-menu-width-min) * 1.4) - 1px);
          }
        }

      }
    }

    :deep() {
      .vip-logo-column {
        .logo {
          width: calc(var(--el-left-menu-width-min) * 1.4) !important;
        }

        .title {
          left: calc(var(--el-left-menu-width-min) * 1.4) !important;
          width: calc(var(--el-left-menu-width) - calc(var(--el-left-menu-width-min) * 1.4) - 1px);
        }
      }
    }
  }

  &-card {
    :deep() {
      .el-tabs {
        .el-tabs__item {
          padding: 5px !important;

          .vip-column-grid {
            width: calc(var(--el-left-menu-width-min) - 12px) !important;
            height: calc(var(--el-left-menu-width-min) - 10px) !important;
            margin-left: 2px;
            border-radius: var(--el-border-radius-base);

            &:hover {
              background: var(--el-color-primary);
            }
          }

          &.is-active {
            background: transparent !important;

            .vip-column-grid {
              background: var(--el-color-primary);
            }
          }
        }
      }

      .vip-column-bar-right {
        padding-left: 10px;
      }

      .el-menu {
        width: calc(var(--el-left-menu-width) - var(--el-left-menu-width-min) - 20px);
      }

      .el-sub-menu .el-sub-menu__title,
      .el-menu-item {
        min-width: 180px;
        margin-bottom: 5px;
        border-radius: var(--el-border-radius-base);
      }
    }
  }

  &-arrow {
    :deep() {
      .el-tabs {
        .el-tabs__item {
          &.is-active {
            background: transparent !important;

            .vip-column-grid {
              background: transparent !important;

              &:after {
                position: absolute;
                right: -1px;
                width: 0;
                height: 0;
                overflow: hidden;
                content: '';
                border-color: transparent var(--el-color-white) transparent transparent;
                border-style: solid dashed dashed;
                border-width: 8px;
              }
            }
          }
        }
      }

      //.el-tabs + .el-menu {
      //  left: calc(var(--el-left-menu-width-min) + 10px);
      //  width: calc(var(--el-left-menu-width) - var(--el-left-menu-width-min) - 20px);
      //}

      .el-sub-menu .el-sub-menu__title,
      .el-menu-item {
        min-width: 180px;
        margin-bottom: 5px;
        border-radius: var(--el-border-radius-base);
      }
    }
  }

  &-semicircle {
    :deep() {
      .el-tabs {
        .el-tabs__item {
          &.is-active {
            //border-top-left-radius: 99px;
            //border-bottom-left-radius: 99px;
            border-bottom-right-radius: 99px;
            border-top-right-radius: 99px;
          }
        }
      }
    }
  }

  .vip-column-grid {
    display: flex;
    align-items: center;
    width: var(--el-left-menu-width-min);
    overflow: hidden;
    text-align: center;
    text-overflow: ellipsis;
    word-break: break-all;
    white-space: nowrap;

    &-vertical,
    &-card,
    &-arrow {
      justify-content: center;
      height: var(--el-left-menu-width-min);

      > div {
        svg,
        [class*='ri-'] {
          display: block;
          height: 20px;
        }
      }
    }

    &-horizontal,
    &-semicircle {
      justify-content: center;
      width: calc(var(--el-left-menu-width-min) * 1.4);
      height: calc(var(--el-left-menu-width-min) / 1.4);
      padding-left: var(--el-padding);

      svg,
      [class*='ri-'] {
        margin-right: 3px;
      }
    }
  }

  :deep() {
    * {
      transition: var(--el-transition);
    }

    .el-scrollbar__wrap {
      overflow-x: hidden;
    }

    .el-tabs {
      //position: fixed;
      z-index: 9999;

      .el-tabs__header.is-left {
        margin-right: 0 !important;

        .el-tabs__nav-wrap.is-left {
          margin-right: 0 !important;
          background: var(--el-menu-background-color);

          .el-tabs__nav-scroll {
            height: 100%;
            overflow-y: auto;

            &::-webkit-scrollbar {
              width: 0;
              height: 0;
            }
          }
        }
      }

      .el-tabs__nav {
        height: calc(100vh - var(--el-logo-height));
        background: var(--el-menu-background-color);
      }

      .el-tabs__item {
        height: auto;
        padding: 0;
        color: var(--el-color-white);

        &.is-active {
          margin-right: -1px;
          background: var(--el-color-primary);

          > .vip-column-grid {
            margin-right: 1px;
          }
        }
      }
    }

    .el-tabs__active-bar.is-left,
    .el-tabs--left .el-tabs__nav-wrap.is-left::after {
      display: none;
    }

    .el-menu {
      //position: fixed;
      margin-top: 10px;
      border: 0;

      .el-menu-item,
      .el-sub-menu__title {
        height: var(--el-menu-item-height);
        overflow: hidden;
        line-height: var(--el-menu-item-height);
        text-overflow: ellipsis;
        white-space: nowrap;

        @include active;
      }
    }
  }

  //&.is-collapse {
  //  :deep {
  //    //width: 0;
  //    width: calc(var(--el-left-menu-width-min));
  //  }
  //}
}

</style>
